<?php

/*
 * SeonameToId
 *
 */

class ModelToolCustom extends Model {

    //Get id from seo name
    function SeonameToId($tablename, $seoname, $fid='id', $fseoname='seo_name') {
        //Get value
        $query = "SELECT %s FROM ".DB_PREFIX."%s"
                . "\n WHERE %s = '%s'"
                . "\n LIMIT 0,1";
        $query = $this->db->query(( sprintf($query, $fid, $tablename, $fseoname, $seoname)));
        return $query->rows[0][$fid];
    }

    public function treeCategoryNews($parentid = 0, $dept = "--", $tree = "") {
        $query = "SELECT *
                  FROM " . DB_PREFIX . "category_news nc INNER JOIN " . DB_PREFIX . "category_news_description ncd ON nc.category_news_id=ncd.category_news_id
                  WHERE ncd.language_id=" . $this->config->get('config_language_id') . " AND nc.parent_news_id=" . $parentid;
        $result = $this->db->query($query);
        foreach ($result->rows as $item) {
            $tree[] = array(
                'category_news_id' => $item['category_news_id'],
                'name' => $item['name'],
                'seo_name' => $item['seo_name'],
                'parentid' => $item['parent_news_id']
            );
            $tree = $this->treeCategoryNews($parentid = $item['category_news_id'], $dept . "--", $tree);
        }

        return $tree;
    }
}

?>
